﻿<template>
  <div class = "login_bgContent" v-loading = 'loading' element-loading-text = '用户信息获取中...'>
    <!--背景图-->
    <img src = "static/style/common/images/loginBg.jpg" alt = "图片路径不正确">
    <!-- 上部的logo -->
    <div class = "login_logo">
      <img src = "static/style/common/images/loginLogo.png" alt="">
      <span>新华书店荐购平台</span>
    </div>
    <div class = 'login_loadingText'>获取用户信息中...</div>
    <!-- 底部的文字 -->
    <div class="login_footer">2021©武汉飞加网络科技. ALL Rights Reserved.</div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      loading: false,
      loadingText: '用户信息获取中...'
    }
  },
  created () {
    this.getLoginInfo()
  },
  methods: {
    getLoginInfo () {
      this.loading = true
      this.loadingText = '用户信息获取中...'
      if (window.location.href.indexOf('?_token') > 0) {
        let tokenInfo = window.location.href.slice(window.location.href.indexOf('_token=') + 7)
        const list = tokenInfo.split('.')
        const str = atob(list[1].replace(/-/g, '+').replace(/_/g, '/'))
        const loginInfo = JSON.parse(str)
        if (sessionStorage) {
          sessionStorage.clear()
        }
        if (loginInfo) {
          // 登录账号的name
          if (loginInfo.name) {
            sessionStorage.setItem('userName', decodeURIComponent(loginInfo.name))
          } else {
            if (loginInfo.usertype === 'System') {
              sessionStorage.setItem('userName', '系统管理员')
            } else {
              sessionStorage.setItem('userName', '佚名')
            }
          }
          // 用户的uid，更换密码的时候会使用(登录者的唯一编码)
          sessionStorage.setItem('userUid', loginInfo.Login_UID)
          // 用户类型(R999为超级管理员)
          sessionStorage.setItem('userType', loginInfo.usertype)
          // 用户编号
          sessionStorage.setItem('userNo', loginInfo.userno)
          // 登录者的角色(职位)编号，多个角色已逗号分隔，GM--经理
          sessionStorage.setItem('roleNo', loginInfo.roleno)
          // 执行流程时对应的变量为 $_login_id
          sessionStorage.setItem('nameId', loginInfo.nameid)
          // 用户图片
          sessionStorage.setItem('avatar', loginInfo.avatar)
          // 登录成功后，改变state中的值
          this.$store.dispatch('isLogin', loginInfo)
        }
        sessionStorage.setItem('token', tokenInfo)
        // 进行注册操作
        this.registerMethod()
      } else {
        this.$message({
          message: '没有获取到用户信息',
          type: 'warning',
          duration: 3000
        })
        this.loading = false
      }
    },
    // 注册的功能
    registerMethod () {
      this.$patch('/sso/register').then(data => {
        let dataResult = data.data.result
        if (dataResult.code === 'success') {
          this.loadingText = '用户权限获取中...'
          this.getMenu()
        } else {
          this.$message({
            message: '没有获取到用户信息',
            type: 'warning',
            duration: 3000
          })
        }
        this.loading = false
      }).catch(error => {
        console.log(error)
        this.loading = false
      })
    },
    // 获取用户权限
    getMenu () {
      this.$get('/menu').then(data => {
        if (data.status === 200) {
          if (data.data.result.length) {
            let toUrl = ''
            let routerNavList = []
            for (let i = 0; i < data.data.result.length; i++) {
              if (data.data.result[i].function_url && data.data.result[i].url_type === 'Router') {
                toUrl = toUrl || data.data.result[i].function_url
                routerNavList.push(data.data.result[i])
              }
            }
            sessionStorage.setItem('routerNavList', (routerNavList && routerNavList.length) ? JSON.stringify(routerNavList) : '')
            if (toUrl) {
              this.$router.push(toUrl)
            } else {
              this.$message({
                message: '请检查配置的功能地址（function_url）是否正确',
                type: 'warning'
              })
            }
          } else {
            this.$message({
              message: '该账号暂无功能权限，请联系管理员添加权限',
              type: 'warning'
            })
          }
        }
        this.loading = false
      }).catch(error => {
        console.log(error)
        this.loading = false
      })
    }
  }
}
</script>

<style lang="scss" scoped>
/*登录页面的背景图片*/
.login_bgContent{
  position:fixed;
  height:100%;
  width:100%;
  z-index:100;
}
.login_bgContent>img{
  display: block;
  width:100%;
  height:100%;
}
.login_logo{
  width:661px !important;
  height:66px !important;
  line-height:66px !important;
  position:absolute;
  left:50% !important;
  top:50%;
  margin-top:-330px;
  margin-left:-330px;
  transform: translateX(0) !important;
  padding-left: 133px;
  img{
   float: left;
   height: 65px !important;
   width: 65px !important;
  }
  span{
    float: left;
    font-size: 40px;
    color: #fff;
    margin-left: 10px;
  }
}
.login_footer{
  width:600px;
  height:24px;
  line-height:24px;
  text-align:center;
  position:absolute;
  left:50%;
  top:50%;
  margin-top:330px;
  color:#fff;
}
.login_loadingText {
  position: absolute;
  left: 50%;
  top: 50%;
  color: #fff;
  font-size: 16px;
  margin-left: -60px;
  margin-top: -10px;
}
@media(max-width:1367px) {
  .login_footer{
    margin-top:240px;
  }
  .login_logo{
    margin-top:-280px;
  }
}
</style>
